package com.mszlu.blog.controller;


import com.mszlu.blog.common.aop.LogAnnotation;
import com.mszlu.blog.common.cache.Cache;
import com.mszlu.blog.service.ArticleService;
import com.mszlu.blog.vo.Result;
import com.mszlu.blog.vo.params.ArticleParam;
import com.mszlu.blog.vo.params.PageParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//json数据进行交互
@RestController
@RequestMapping("/articles")
public class ArticleController {
    @Autowired
    ArticleService articleService;
    //首页文章列表
    @ApiOperation(value="首页文章列表")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK", response = Result.class) })
    @PostMapping
    //加上此注解代表要对此接口记录日志
    @LogAnnotation(module="文章",operator="获取文章列表")
    @Cache(expire = 5*60*1000,name = "listArticle")
    public Result listArticle(@RequestBody PageParams pageParams) {
        return articleService.listArticle(pageParams);
    }

    /**
     * 首页 最热文章
     * @return
     */

    @ApiOperation(value="最热文章")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK", response = Result.class) })
    @PostMapping("hot")
    @Cache(expire = 5*60*1000,name = "hot_article")
    public Result hotArticle() {
        int limit =5;
        return articleService.hotArticle(limit);
    }

    /**
     * 首页 最新文章
     * @return
     */
    @ApiOperation(value="最新文章")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK", response = Result.class) })
    @PostMapping("new")
    public Result newArticle() {
        int limit =5;
        return articleService.newArticle(limit);
    }

    /**
     * 文章归档
     */
    @ApiOperation(value="文章归档")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK", response = Result.class) })
    @PostMapping("listArchives")
    public Result listArchives() {
        int limit =5;
        return articleService.listArchives(limit);
    }

    @ApiOperation(value="查看文章")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK", response = Result.class) })
    @PostMapping("view/{id}")
    public Result findArticleId(@PathVariable("id")Long articleId){
        return articleService.findArticleId(articleId);
    }
    /**
    *发布文章
    */
    @ApiOperation(value="发部文章")
    @ApiResponses({ @ApiResponse(code = 200, message = "OK", response = Result.class) })
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);


    }

}

















